{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "9b309adf-7b4c-4caf-8894-01c31df6d61e",
   "metadata": {},
   "outputs": [],
   "source": [
    "import sklearn"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5609821f-7041-4d02-a98c-624ffe431a78",
   "metadata": {},
   "source": [
    "# 创建数据集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "da377b1d-ed8b-4a95-a1d9-411952ca4765",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn import datasets\n",
    "iris = datasets.load_iris()\n",
    "X = iris.data\n",
    "y = iris.target\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "83237cf6-6892-4b7c-8e49-3123c680b3da",
   "metadata": {},
   "source": [
    "# 划分数据集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "e16842fc-6b34-4dee-8c7d-54bbe4c313b8",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.model_selection import train_test_split\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e9158ff6-7367-4b2f-b6d0-35aeed0196df",
   "metadata": {},
   "source": [
    "# 训练模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "9ab3bc3d-f116-4e6d-bdd6-6b5752ca2419",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<style>#sk-container-id-1 {color: black;background-color: white;}#sk-container-id-1 pre{padding: 0;}#sk-container-id-1 div.sk-toggleable {background-color: white;}#sk-container-id-1 label.sk-toggleable__label {cursor: pointer;display: block;width: 100%;margin-bottom: 0;padding: 0.3em;box-sizing: border-box;text-align: center;}#sk-container-id-1 label.sk-toggleable__label-arrow:before {content: \"▸\";float: left;margin-right: 0.25em;color: #696969;}#sk-container-id-1 label.sk-toggleable__label-arrow:hover:before {color: black;}#sk-container-id-1 div.sk-estimator:hover label.sk-toggleable__label-arrow:before {color: black;}#sk-container-id-1 div.sk-toggleable__content {max-height: 0;max-width: 0;overflow: hidden;text-align: left;background-color: #f0f8ff;}#sk-container-id-1 div.sk-toggleable__content pre {margin: 0.2em;color: black;border-radius: 0.25em;background-color: #f0f8ff;}#sk-container-id-1 input.sk-toggleable__control:checked~div.sk-toggleable__content {max-height: 200px;max-width: 100%;overflow: auto;}#sk-container-id-1 input.sk-toggleable__control:checked~label.sk-toggleable__label-arrow:before {content: \"▾\";}#sk-container-id-1 div.sk-estimator input.sk-toggleable__control:checked~label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-1 div.sk-label input.sk-toggleable__control:checked~label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-1 input.sk-hidden--visually {border: 0;clip: rect(1px 1px 1px 1px);clip: rect(1px, 1px, 1px, 1px);height: 1px;margin: -1px;overflow: hidden;padding: 0;position: absolute;width: 1px;}#sk-container-id-1 div.sk-estimator {font-family: monospace;background-color: #f0f8ff;border: 1px dotted black;border-radius: 0.25em;box-sizing: border-box;margin-bottom: 0.5em;}#sk-container-id-1 div.sk-estimator:hover {background-color: #d4ebff;}#sk-container-id-1 div.sk-parallel-item::after {content: \"\";width: 100%;border-bottom: 1px solid gray;flex-grow: 1;}#sk-container-id-1 div.sk-label:hover label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-1 div.sk-serial::before {content: \"\";position: absolute;border-left: 1px solid gray;box-sizing: border-box;top: 0;bottom: 0;left: 50%;z-index: 0;}#sk-container-id-1 div.sk-serial {display: flex;flex-direction: column;align-items: center;background-color: white;padding-right: 0.2em;padding-left: 0.2em;position: relative;}#sk-container-id-1 div.sk-item {position: relative;z-index: 1;}#sk-container-id-1 div.sk-parallel {display: flex;align-items: stretch;justify-content: center;background-color: white;position: relative;}#sk-container-id-1 div.sk-item::before, #sk-container-id-1 div.sk-parallel-item::before {content: \"\";position: absolute;border-left: 1px solid gray;box-sizing: border-box;top: 0;bottom: 0;left: 50%;z-index: -1;}#sk-container-id-1 div.sk-parallel-item {display: flex;flex-direction: column;z-index: 1;position: relative;background-color: white;}#sk-container-id-1 div.sk-parallel-item:first-child::after {align-self: flex-end;width: 50%;}#sk-container-id-1 div.sk-parallel-item:last-child::after {align-self: flex-start;width: 50%;}#sk-container-id-1 div.sk-parallel-item:only-child::after {width: 0;}#sk-container-id-1 div.sk-dashed-wrapped {border: 1px dashed gray;margin: 0 0.4em 0.5em 0.4em;box-sizing: border-box;padding-bottom: 0.4em;background-color: white;}#sk-container-id-1 div.sk-label label {font-family: monospace;font-weight: bold;display: inline-block;line-height: 1.2em;}#sk-container-id-1 div.sk-label-container {text-align: center;}#sk-container-id-1 div.sk-container {/* jupyter's `normalize.less` sets `[hidden] { display: none; }` but bootstrap.min.css set `[hidden] { display: none !important; }` so we also need the `!important` here to be able to override the default hidden behavior on the sphinx rendered scikit-learn.org. See: https://github.com/scikit-learn/scikit-learn/issues/21755 */display: inline-block !important;position: relative;}#sk-container-id-1 div.sk-text-repr-fallback {display: none;}</style><div id=\"sk-container-id-1\" class=\"sk-top-container\"><div class=\"sk-text-repr-fallback\"><pre>KNeighborsClassifier()</pre><b>In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook. <br />On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.</b></div><div class=\"sk-container\" hidden><div class=\"sk-item\"><div class=\"sk-estimator sk-toggleable\"><input class=\"sk-toggleable__control sk-hidden--visually\" id=\"sk-estimator-id-1\" type=\"checkbox\" checked><label for=\"sk-estimator-id-1\" class=\"sk-toggleable__label sk-toggleable__label-arrow\">KNeighborsClassifier</label><div class=\"sk-toggleable__content\"><pre>KNeighborsClassifier()</pre></div></div></div></div></div>"
      ],
      "text/plain": [
       "KNeighborsClassifier()"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sklearn.neighbors import KNeighborsClassifier\n",
    "knn = KNeighborsClassifier()\n",
    "knn.fit(X_train, y_train)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "67296659-dd92-42da-b164-2e25eb9efc37",
   "metadata": {},
   "source": [
    "# 评估模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "8aaeca72-0d04-442f-8d0f-512e9c580092",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "准确率：0.9736842105263158\n"
     ]
    }
   ],
   "source": [
    "from sklearn.metrics import accuracy_score\n",
    "y_pred = knn.predict(X_test)\n",
    "accuracy = accuracy_score(y_test,y_pred)\n",
    "print(f'准确率：{accuracy}')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1bcb3c38-ae75-4082-a206-efd5fe304642",
   "metadata": {},
   "source": [
    "# 保存模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "952b4a28-093d-4bfb-ab0c-d20edf8d3e4a",
   "metadata": {},
   "outputs": [
    {
     "ename": "ImportError",
     "evalue": "cannot import name 'joblib' from 'sklearn.externals' (C:\\Users\\YFB-SERVER\\AppData\\Local\\miniconda3\\Lib\\site-packages\\sklearn\\externals\\__init__.py)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mImportError\u001b[0m                               Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[11], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01msklearn\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mexternals\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m joblib\n\u001b[0;32m      2\u001b[0m joblib\u001b[38;5;241m.\u001b[39mdump(knn, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mknn.pkl\u001b[39m\u001b[38;5;124m'\u001b[39m)\n",
      "\u001b[1;31mImportError\u001b[0m: cannot import name 'joblib' from 'sklearn.externals' (C:\\Users\\YFB-SERVER\\AppData\\Local\\miniconda3\\Lib\\site-packages\\sklearn\\externals\\__init__.py)"
     ]
    }
   ],
   "source": [
    "from sklearn.externals import joblib\n",
    "joblib.dump(knn, 'knn.pkl')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "708fd21c-6ba1-4854-b0d0-11e1fd326f59",
   "metadata": {},
   "source": [
    "# 十分钟|通过sklearn上手你的第一个机器学习实例"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "a38f06a7-a849-4faa-ac24-26b6aef866ec",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'1.2.2'"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import sklearn\n",
    "sklearn.__version__"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "bfe3796e-e981-4d99-92b9-1449795a85d1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['DESCR',\n",
       " 'data',\n",
       " 'data_module',\n",
       " 'feature_names',\n",
       " 'filename',\n",
       " 'frame',\n",
       " 'target',\n",
       " 'target_names']"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import sklearn.datasets as sk_datasets\n",
    "iris = sk_datasets.load_iris()\n",
    "iris_X = iris.data  # 导入数据\n",
    "iris_y = iris.target # 导入标签\n",
    "\n",
    "from sklearn.datasets import load_iris\n",
    "dir(load_iris())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "0f63ee52-d71a-4fff-b3f3-4cbd6f6c522b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'.. _iris_dataset:\\n\\nIris plants dataset\\n--------------------\\n\\n**Data Set Characteristics:**\\n\\n    :Number of Instances: 150 (50 in each of three classes)\\n    :Number of Attributes: 4 numeric, predictive attributes and the class\\n    :Attribute Information:\\n        - sepal length in cm\\n        - sepal width in cm\\n        - petal length in cm\\n        - petal width in cm\\n        - class:\\n                - Iris-Setosa\\n                - Iris-Versicolour\\n                - Iris-Virginica\\n                \\n    :Summary Statistics:\\n\\n    ============== ==== ==== ======= ===== ====================\\n                    Min  Max   Mean    SD   Class Correlation\\n    ============== ==== ==== ======= ===== ====================\\n    sepal length:   4.3  7.9   5.84   0.83    0.7826\\n    sepal width:    2.0  4.4   3.05   0.43   -0.4194\\n    petal length:   1.0  6.9   3.76   1.76    0.9490  (high!)\\n    petal width:    0.1  2.5   1.20   0.76    0.9565  (high!)\\n    ============== ==== ==== ======= ===== ====================\\n\\n    :Missing Attribute Values: None\\n    :Class Distribution: 33.3% for each of 3 classes.\\n    :Creator: R.A. Fisher\\n    :Donor: Michael Marshall (MARSHALL%PLU@io.arc.nasa.gov)\\n    :Date: July, 1988\\n\\nThe famous Iris database, first used by Sir R.A. Fisher. The dataset is taken\\nfrom Fisher\\'s paper. Note that it\\'s the same as in R, but not as in the UCI\\nMachine Learning Repository, which has two wrong data points.\\n\\nThis is perhaps the best known database to be found in the\\npattern recognition literature.  Fisher\\'s paper is a classic in the field and\\nis referenced frequently to this day.  (See Duda & Hart, for example.)  The\\ndata set contains 3 classes of 50 instances each, where each class refers to a\\ntype of iris plant.  One class is linearly separable from the other 2; the\\nlatter are NOT linearly separable from each other.\\n\\n.. topic:: References\\n\\n   - Fisher, R.A. \"The use of multiple measurements in taxonomic problems\"\\n     Annual Eugenics, 7, Part II, 179-188 (1936); also in \"Contributions to\\n     Mathematical Statistics\" (John Wiley, NY, 1950).\\n   - Duda, R.O., & Hart, P.E. (1973) Pattern Classification and Scene Analysis.\\n     (Q327.D83) John Wiley & Sons.  ISBN 0-471-22361-1.  See page 218.\\n   - Dasarathy, B.V. (1980) \"Nosing Around the Neighborhood: A New System\\n     Structure and Classification Rule for Recognition in Partially Exposed\\n     Environments\".  IEEE Transactions on Pattern Analysis and Machine\\n     Intelligence, Vol. PAMI-2, No. 1, 67-71.\\n   - Gates, G.W. (1972) \"The Reduced Nearest Neighbor Rule\".  IEEE Transactions\\n     on Information Theory, May 1972, 431-433.\\n   - See also: 1988 MLC Proceedings, 54-64.  Cheeseman et al\"s AUTOCLASS II\\n     conceptual clustering system finds 3 classes in the data.\\n   - Many, many more ...'"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "load_iris().DESCR"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "20c40b83-580a-48ad-b0cd-d9c3256aa511",
   "metadata": {},
   "source": [
    "## 数据预处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "1f7f2671-eb77-44e5-bd68-762b50e32575",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn import preprocessing\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "973183e4-c90c-4037-99e7-c4380bf6c6e3",
   "metadata": {},
   "source": [
    "### 区间缩放   url :https://zhuanlan.zhihu.com/p/80186372"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "2b856321-050d-40b8-b32e-d4341f021835",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.preprocessing import MinMaxScaler\n",
    "scaler = MinMaxScaler(feature_range=(0,1))\n",
    "iris_X = scaler.fit_transform(iris_X)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6c7a4dc6-53d0-4d8b-99ed-59539aa1e32c",
   "metadata": {},
   "source": [
    "### 标准化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "556a1c8e-d3b4-4199-a95d-1162915c1dfb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-9.00681170e-01,  1.01900435e+00, -1.34022653e+00,\n",
       "        -1.31544430e+00],\n",
       "       [-1.14301691e+00, -1.31979479e-01, -1.34022653e+00,\n",
       "        -1.31544430e+00],\n",
       "       [-1.38535265e+00,  3.28414053e-01, -1.39706395e+00,\n",
       "        -1.31544430e+00],\n",
       "       [-1.50652052e+00,  9.82172869e-02, -1.28338910e+00,\n",
       "        -1.31544430e+00],\n",
       "       [-1.02184904e+00,  1.24920112e+00, -1.34022653e+00,\n",
       "        -1.31544430e+00],\n",
       "       [-5.37177559e-01,  1.93979142e+00, -1.16971425e+00,\n",
       "        -1.05217993e+00],\n",
       "       [-1.50652052e+00,  7.88807586e-01, -1.34022653e+00,\n",
       "        -1.18381211e+00],\n",
       "       [-1.02184904e+00,  7.88807586e-01, -1.28338910e+00,\n",
       "        -1.31544430e+00],\n",
       "       [-1.74885626e+00, -3.62176246e-01, -1.34022653e+00,\n",
       "        -1.31544430e+00],\n",
       "       [-1.14301691e+00,  9.82172869e-02, -1.28338910e+00,\n",
       "        -1.44707648e+00],\n",
       "       [-5.37177559e-01,  1.47939788e+00, -1.28338910e+00,\n",
       "        -1.31544430e+00],\n",
       "       [-1.26418478e+00,  7.88807586e-01, -1.22655167e+00,\n",
       "        -1.31544430e+00],\n",
       "       [-1.26418478e+00, -1.31979479e-01, -1.34022653e+00,\n",
       "        -1.44707648e+00],\n",
       "       [-1.87002413e+00, -1.31979479e-01, -1.51073881e+00,\n",
       "        -1.44707648e+00],\n",
       "       [-5.25060772e-02,  2.16998818e+00, -1.45390138e+00,\n",
       "        -1.31544430e+00],\n",
       "       [-1.73673948e-01,  3.09077525e+00, -1.28338910e+00,\n",
       "        -1.05217993e+00],\n",
       "       [-5.37177559e-01,  1.93979142e+00, -1.39706395e+00,\n",
       "        -1.05217993e+00],\n",
       "       [-9.00681170e-01,  1.01900435e+00, -1.34022653e+00,\n",
       "        -1.18381211e+00],\n",
       "       [-1.73673948e-01,  1.70959465e+00, -1.16971425e+00,\n",
       "        -1.18381211e+00],\n",
       "       [-9.00681170e-01,  1.70959465e+00, -1.28338910e+00,\n",
       "        -1.18381211e+00],\n",
       "       [-5.37177559e-01,  7.88807586e-01, -1.16971425e+00,\n",
       "        -1.31544430e+00],\n",
       "       [-9.00681170e-01,  1.47939788e+00, -1.28338910e+00,\n",
       "        -1.05217993e+00],\n",
       "       [-1.50652052e+00,  1.24920112e+00, -1.56757623e+00,\n",
       "        -1.31544430e+00],\n",
       "       [-9.00681170e-01,  5.58610819e-01, -1.16971425e+00,\n",
       "        -9.20547742e-01],\n",
       "       [-1.26418478e+00,  7.88807586e-01, -1.05603939e+00,\n",
       "        -1.31544430e+00],\n",
       "       [-1.02184904e+00, -1.31979479e-01, -1.22655167e+00,\n",
       "        -1.31544430e+00],\n",
       "       [-1.02184904e+00,  7.88807586e-01, -1.22655167e+00,\n",
       "        -1.05217993e+00],\n",
       "       [-7.79513300e-01,  1.01900435e+00, -1.28338910e+00,\n",
       "        -1.31544430e+00],\n",
       "       [-7.79513300e-01,  7.88807586e-01, -1.34022653e+00,\n",
       "        -1.31544430e+00],\n",
       "       [-1.38535265e+00,  3.28414053e-01, -1.22655167e+00,\n",
       "        -1.31544430e+00],\n",
       "       [-1.26418478e+00,  9.82172869e-02, -1.22655167e+00,\n",
       "        -1.31544430e+00],\n",
       "       [-5.37177559e-01,  7.88807586e-01, -1.28338910e+00,\n",
       "        -1.05217993e+00],\n",
       "       [-7.79513300e-01,  2.40018495e+00, -1.28338910e+00,\n",
       "        -1.44707648e+00],\n",
       "       [-4.16009689e-01,  2.63038172e+00, -1.34022653e+00,\n",
       "        -1.31544430e+00],\n",
       "       [-1.14301691e+00,  9.82172869e-02, -1.28338910e+00,\n",
       "        -1.31544430e+00],\n",
       "       [-1.02184904e+00,  3.28414053e-01, -1.45390138e+00,\n",
       "        -1.31544430e+00],\n",
       "       [-4.16009689e-01,  1.01900435e+00, -1.39706395e+00,\n",
       "        -1.31544430e+00],\n",
       "       [-1.14301691e+00,  1.24920112e+00, -1.34022653e+00,\n",
       "        -1.44707648e+00],\n",
       "       [-1.74885626e+00, -1.31979479e-01, -1.39706395e+00,\n",
       "        -1.31544430e+00],\n",
       "       [-9.00681170e-01,  7.88807586e-01, -1.28338910e+00,\n",
       "        -1.31544430e+00],\n",
       "       [-1.02184904e+00,  1.01900435e+00, -1.39706395e+00,\n",
       "        -1.18381211e+00],\n",
       "       [-1.62768839e+00, -1.74335684e+00, -1.39706395e+00,\n",
       "        -1.18381211e+00],\n",
       "       [-1.74885626e+00,  3.28414053e-01, -1.39706395e+00,\n",
       "        -1.31544430e+00],\n",
       "       [-1.02184904e+00,  1.01900435e+00, -1.22655167e+00,\n",
       "        -7.88915558e-01],\n",
       "       [-9.00681170e-01,  1.70959465e+00, -1.05603939e+00,\n",
       "        -1.05217993e+00],\n",
       "       [-1.26418478e+00, -1.31979479e-01, -1.34022653e+00,\n",
       "        -1.18381211e+00],\n",
       "       [-9.00681170e-01,  1.70959465e+00, -1.22655167e+00,\n",
       "        -1.31544430e+00],\n",
       "       [-1.50652052e+00,  3.28414053e-01, -1.34022653e+00,\n",
       "        -1.31544430e+00],\n",
       "       [-6.58345429e-01,  1.47939788e+00, -1.28338910e+00,\n",
       "        -1.31544430e+00],\n",
       "       [-1.02184904e+00,  5.58610819e-01, -1.34022653e+00,\n",
       "        -1.31544430e+00],\n",
       "       [ 1.40150837e+00,  3.28414053e-01,  5.35408562e-01,\n",
       "         2.64141916e-01],\n",
       "       [ 6.74501145e-01,  3.28414053e-01,  4.21733708e-01,\n",
       "         3.95774101e-01],\n",
       "       [ 1.28034050e+00,  9.82172869e-02,  6.49083415e-01,\n",
       "         3.95774101e-01],\n",
       "       [-4.16009689e-01, -1.74335684e+00,  1.37546573e-01,\n",
       "         1.32509732e-01],\n",
       "       [ 7.95669016e-01, -5.92373012e-01,  4.78571135e-01,\n",
       "         3.95774101e-01],\n",
       "       [-1.73673948e-01, -5.92373012e-01,  4.21733708e-01,\n",
       "         1.32509732e-01],\n",
       "       [ 5.53333275e-01,  5.58610819e-01,  5.35408562e-01,\n",
       "         5.27406285e-01],\n",
       "       [-1.14301691e+00, -1.51316008e+00, -2.60315415e-01,\n",
       "        -2.62386821e-01],\n",
       "       [ 9.16836886e-01, -3.62176246e-01,  4.78571135e-01,\n",
       "         1.32509732e-01],\n",
       "       [-7.79513300e-01, -8.22569778e-01,  8.07091462e-02,\n",
       "         2.64141916e-01],\n",
       "       [-1.02184904e+00, -2.43394714e+00, -1.46640561e-01,\n",
       "        -2.62386821e-01],\n",
       "       [ 6.86617933e-02, -1.31979479e-01,  2.51221427e-01,\n",
       "         3.95774101e-01],\n",
       "       [ 1.89829664e-01, -1.97355361e+00,  1.37546573e-01,\n",
       "        -2.62386821e-01],\n",
       "       [ 3.10997534e-01, -3.62176246e-01,  5.35408562e-01,\n",
       "         2.64141916e-01],\n",
       "       [-2.94841818e-01, -3.62176246e-01, -8.98031345e-02,\n",
       "         1.32509732e-01],\n",
       "       [ 1.03800476e+00,  9.82172869e-02,  3.64896281e-01,\n",
       "         2.64141916e-01],\n",
       "       [-2.94841818e-01, -1.31979479e-01,  4.21733708e-01,\n",
       "         3.95774101e-01],\n",
       "       [-5.25060772e-02, -8.22569778e-01,  1.94384000e-01,\n",
       "        -2.62386821e-01],\n",
       "       [ 4.32165405e-01, -1.97355361e+00,  4.21733708e-01,\n",
       "         3.95774101e-01],\n",
       "       [-2.94841818e-01, -1.28296331e+00,  8.07091462e-02,\n",
       "        -1.30754636e-01],\n",
       "       [ 6.86617933e-02,  3.28414053e-01,  5.92245988e-01,\n",
       "         7.90670654e-01],\n",
       "       [ 3.10997534e-01, -5.92373012e-01,  1.37546573e-01,\n",
       "         1.32509732e-01],\n",
       "       [ 5.53333275e-01, -1.28296331e+00,  6.49083415e-01,\n",
       "         3.95774101e-01],\n",
       "       [ 3.10997534e-01, -5.92373012e-01,  5.35408562e-01,\n",
       "         8.77547895e-04],\n",
       "       [ 6.74501145e-01, -3.62176246e-01,  3.08058854e-01,\n",
       "         1.32509732e-01],\n",
       "       [ 9.16836886e-01, -1.31979479e-01,  3.64896281e-01,\n",
       "         2.64141916e-01],\n",
       "       [ 1.15917263e+00, -5.92373012e-01,  5.92245988e-01,\n",
       "         2.64141916e-01],\n",
       "       [ 1.03800476e+00, -1.31979479e-01,  7.05920842e-01,\n",
       "         6.59038469e-01],\n",
       "       [ 1.89829664e-01, -3.62176246e-01,  4.21733708e-01,\n",
       "         3.95774101e-01],\n",
       "       [-1.73673948e-01, -1.05276654e+00, -1.46640561e-01,\n",
       "        -2.62386821e-01],\n",
       "       [-4.16009689e-01, -1.51316008e+00,  2.38717193e-02,\n",
       "        -1.30754636e-01],\n",
       "       [-4.16009689e-01, -1.51316008e+00, -3.29657076e-02,\n",
       "        -2.62386821e-01],\n",
       "       [-5.25060772e-02, -8.22569778e-01,  8.07091462e-02,\n",
       "         8.77547895e-04],\n",
       "       [ 1.89829664e-01, -8.22569778e-01,  7.62758269e-01,\n",
       "         5.27406285e-01],\n",
       "       [-5.37177559e-01, -1.31979479e-01,  4.21733708e-01,\n",
       "         3.95774101e-01],\n",
       "       [ 1.89829664e-01,  7.88807586e-01,  4.21733708e-01,\n",
       "         5.27406285e-01],\n",
       "       [ 1.03800476e+00,  9.82172869e-02,  5.35408562e-01,\n",
       "         3.95774101e-01],\n",
       "       [ 5.53333275e-01, -1.74335684e+00,  3.64896281e-01,\n",
       "         1.32509732e-01],\n",
       "       [-2.94841818e-01, -1.31979479e-01,  1.94384000e-01,\n",
       "         1.32509732e-01],\n",
       "       [-4.16009689e-01, -1.28296331e+00,  1.37546573e-01,\n",
       "         1.32509732e-01],\n",
       "       [-4.16009689e-01, -1.05276654e+00,  3.64896281e-01,\n",
       "         8.77547895e-04],\n",
       "       [ 3.10997534e-01, -1.31979479e-01,  4.78571135e-01,\n",
       "         2.64141916e-01],\n",
       "       [-5.25060772e-02, -1.05276654e+00,  1.37546573e-01,\n",
       "         8.77547895e-04],\n",
       "       [-1.02184904e+00, -1.74335684e+00, -2.60315415e-01,\n",
       "        -2.62386821e-01],\n",
       "       [-2.94841818e-01, -8.22569778e-01,  2.51221427e-01,\n",
       "         1.32509732e-01],\n",
       "       [-1.73673948e-01, -1.31979479e-01,  2.51221427e-01,\n",
       "         8.77547895e-04],\n",
       "       [-1.73673948e-01, -3.62176246e-01,  2.51221427e-01,\n",
       "         1.32509732e-01],\n",
       "       [ 4.32165405e-01, -3.62176246e-01,  3.08058854e-01,\n",
       "         1.32509732e-01],\n",
       "       [-9.00681170e-01, -1.28296331e+00, -4.30827696e-01,\n",
       "        -1.30754636e-01],\n",
       "       [-1.73673948e-01, -5.92373012e-01,  1.94384000e-01,\n",
       "         1.32509732e-01],\n",
       "       [ 5.53333275e-01,  5.58610819e-01,  1.27429511e+00,\n",
       "         1.71209594e+00],\n",
       "       [-5.25060772e-02, -8.22569778e-01,  7.62758269e-01,\n",
       "         9.22302838e-01],\n",
       "       [ 1.52267624e+00, -1.31979479e-01,  1.21745768e+00,\n",
       "         1.18556721e+00],\n",
       "       [ 5.53333275e-01, -3.62176246e-01,  1.04694540e+00,\n",
       "         7.90670654e-01],\n",
       "       [ 7.95669016e-01, -1.31979479e-01,  1.16062026e+00,\n",
       "         1.31719939e+00],\n",
       "       [ 2.12851559e+00, -1.31979479e-01,  1.61531967e+00,\n",
       "         1.18556721e+00],\n",
       "       [-1.14301691e+00, -1.28296331e+00,  4.21733708e-01,\n",
       "         6.59038469e-01],\n",
       "       [ 1.76501198e+00, -3.62176246e-01,  1.44480739e+00,\n",
       "         7.90670654e-01],\n",
       "       [ 1.03800476e+00, -1.28296331e+00,  1.16062026e+00,\n",
       "         7.90670654e-01],\n",
       "       [ 1.64384411e+00,  1.24920112e+00,  1.33113254e+00,\n",
       "         1.71209594e+00],\n",
       "       [ 7.95669016e-01,  3.28414053e-01,  7.62758269e-01,\n",
       "         1.05393502e+00],\n",
       "       [ 6.74501145e-01, -8.22569778e-01,  8.76433123e-01,\n",
       "         9.22302838e-01],\n",
       "       [ 1.15917263e+00, -1.31979479e-01,  9.90107977e-01,\n",
       "         1.18556721e+00],\n",
       "       [-1.73673948e-01, -1.28296331e+00,  7.05920842e-01,\n",
       "         1.05393502e+00],\n",
       "       [-5.25060772e-02, -5.92373012e-01,  7.62758269e-01,\n",
       "         1.58046376e+00],\n",
       "       [ 6.74501145e-01,  3.28414053e-01,  8.76433123e-01,\n",
       "         1.44883158e+00],\n",
       "       [ 7.95669016e-01, -1.31979479e-01,  9.90107977e-01,\n",
       "         7.90670654e-01],\n",
       "       [ 2.24968346e+00,  1.70959465e+00,  1.67215710e+00,\n",
       "         1.31719939e+00],\n",
       "       [ 2.24968346e+00, -1.05276654e+00,  1.78583195e+00,\n",
       "         1.44883158e+00],\n",
       "       [ 1.89829664e-01, -1.97355361e+00,  7.05920842e-01,\n",
       "         3.95774101e-01],\n",
       "       [ 1.28034050e+00,  3.28414053e-01,  1.10378283e+00,\n",
       "         1.44883158e+00],\n",
       "       [-2.94841818e-01, -5.92373012e-01,  6.49083415e-01,\n",
       "         1.05393502e+00],\n",
       "       [ 2.24968346e+00, -5.92373012e-01,  1.67215710e+00,\n",
       "         1.05393502e+00],\n",
       "       [ 5.53333275e-01, -8.22569778e-01,  6.49083415e-01,\n",
       "         7.90670654e-01],\n",
       "       [ 1.03800476e+00,  5.58610819e-01,  1.10378283e+00,\n",
       "         1.18556721e+00],\n",
       "       [ 1.64384411e+00,  3.28414053e-01,  1.27429511e+00,\n",
       "         7.90670654e-01],\n",
       "       [ 4.32165405e-01, -5.92373012e-01,  5.92245988e-01,\n",
       "         7.90670654e-01],\n",
       "       [ 3.10997534e-01, -1.31979479e-01,  6.49083415e-01,\n",
       "         7.90670654e-01],\n",
       "       [ 6.74501145e-01, -5.92373012e-01,  1.04694540e+00,\n",
       "         1.18556721e+00],\n",
       "       [ 1.64384411e+00, -1.31979479e-01,  1.16062026e+00,\n",
       "         5.27406285e-01],\n",
       "       [ 1.88617985e+00, -5.92373012e-01,  1.33113254e+00,\n",
       "         9.22302838e-01],\n",
       "       [ 2.49201920e+00,  1.70959465e+00,  1.50164482e+00,\n",
       "         1.05393502e+00],\n",
       "       [ 6.74501145e-01, -5.92373012e-01,  1.04694540e+00,\n",
       "         1.31719939e+00],\n",
       "       [ 5.53333275e-01, -5.92373012e-01,  7.62758269e-01,\n",
       "         3.95774101e-01],\n",
       "       [ 3.10997534e-01, -1.05276654e+00,  1.04694540e+00,\n",
       "         2.64141916e-01],\n",
       "       [ 2.24968346e+00, -1.31979479e-01,  1.33113254e+00,\n",
       "         1.44883158e+00],\n",
       "       [ 5.53333275e-01,  7.88807586e-01,  1.04694540e+00,\n",
       "         1.58046376e+00],\n",
       "       [ 6.74501145e-01,  9.82172869e-02,  9.90107977e-01,\n",
       "         7.90670654e-01],\n",
       "       [ 1.89829664e-01, -1.31979479e-01,  5.92245988e-01,\n",
       "         7.90670654e-01],\n",
       "       [ 1.28034050e+00,  9.82172869e-02,  9.33270550e-01,\n",
       "         1.18556721e+00],\n",
       "       [ 1.03800476e+00,  9.82172869e-02,  1.04694540e+00,\n",
       "         1.58046376e+00],\n",
       "       [ 1.28034050e+00,  9.82172869e-02,  7.62758269e-01,\n",
       "         1.44883158e+00],\n",
       "       [-5.25060772e-02, -8.22569778e-01,  7.62758269e-01,\n",
       "         9.22302838e-01],\n",
       "       [ 1.15917263e+00,  3.28414053e-01,  1.21745768e+00,\n",
       "         1.44883158e+00],\n",
       "       [ 1.03800476e+00,  5.58610819e-01,  1.10378283e+00,\n",
       "         1.71209594e+00],\n",
       "       [ 1.03800476e+00, -1.31979479e-01,  8.19595696e-01,\n",
       "         1.44883158e+00],\n",
       "       [ 5.53333275e-01, -1.28296331e+00,  7.05920842e-01,\n",
       "         9.22302838e-01],\n",
       "       [ 7.95669016e-01, -1.31979479e-01,  8.19595696e-01,\n",
       "         1.05393502e+00],\n",
       "       [ 4.32165405e-01,  7.88807586e-01,  9.33270550e-01,\n",
       "         1.44883158e+00],\n",
       "       [ 6.86617933e-02, -1.31979479e-01,  7.62758269e-01,\n",
       "         7.90670654e-01]])"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sklearn.preprocessing import StandardScaler\n",
    "StandardScaler().fit_transform(iris.data)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a0638f20-be85-4860-9a18-833f389a3029",
   "metadata": {},
   "source": [
    "### 归一化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "1f366ac7-785d-4301-ab6b-e0675defb81c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.80377277, 0.55160877, 0.22064351, 0.0315205 ],\n",
       "       [0.82813287, 0.50702013, 0.23660939, 0.03380134],\n",
       "       [0.80533308, 0.54831188, 0.2227517 , 0.03426949],\n",
       "       [0.80003025, 0.53915082, 0.26087943, 0.03478392],\n",
       "       [0.790965  , 0.5694948 , 0.2214702 , 0.0316386 ],\n",
       "       [0.78417499, 0.5663486 , 0.2468699 , 0.05808704],\n",
       "       [0.78010936, 0.57660257, 0.23742459, 0.0508767 ],\n",
       "       [0.80218492, 0.54548574, 0.24065548, 0.0320874 ],\n",
       "       [0.80642366, 0.5315065 , 0.25658935, 0.03665562],\n",
       "       [0.81803119, 0.51752994, 0.25041771, 0.01669451],\n",
       "       [0.80373519, 0.55070744, 0.22325977, 0.02976797],\n",
       "       [0.786991  , 0.55745196, 0.26233033, 0.03279129],\n",
       "       [0.82307218, 0.51442011, 0.24006272, 0.01714734],\n",
       "       [0.8025126 , 0.55989251, 0.20529392, 0.01866308],\n",
       "       [0.81120865, 0.55945424, 0.16783627, 0.02797271],\n",
       "       [0.77381111, 0.59732787, 0.2036345 , 0.05430253],\n",
       "       [0.79428944, 0.57365349, 0.19121783, 0.05883625],\n",
       "       [0.80327412, 0.55126656, 0.22050662, 0.04725142],\n",
       "       [0.8068282 , 0.53788547, 0.24063297, 0.04246464],\n",
       "       [0.77964883, 0.58091482, 0.22930848, 0.0458617 ],\n",
       "       [0.8173379 , 0.51462016, 0.25731008, 0.03027177],\n",
       "       [0.78591858, 0.57017622, 0.23115252, 0.06164067],\n",
       "       [0.77577075, 0.60712493, 0.16864581, 0.03372916],\n",
       "       [0.80597792, 0.52151512, 0.26865931, 0.07901744],\n",
       "       [0.776114  , 0.54974742, 0.30721179, 0.03233808],\n",
       "       [0.82647451, 0.4958847 , 0.26447184, 0.03305898],\n",
       "       [0.79778206, 0.5424918 , 0.25529026, 0.06382256],\n",
       "       [0.80641965, 0.54278246, 0.23262105, 0.03101614],\n",
       "       [0.81609427, 0.5336001 , 0.21971769, 0.03138824],\n",
       "       [0.79524064, 0.54144043, 0.27072022, 0.03384003],\n",
       "       [0.80846584, 0.52213419, 0.26948861, 0.03368608],\n",
       "       [0.82225028, 0.51771314, 0.22840286, 0.06090743],\n",
       "       [0.76578311, 0.60379053, 0.22089897, 0.0147266 ],\n",
       "       [0.77867447, 0.59462414, 0.19820805, 0.02831544],\n",
       "       [0.81768942, 0.51731371, 0.25031309, 0.03337508],\n",
       "       [0.82512295, 0.52807869, 0.19802951, 0.03300492],\n",
       "       [0.82699754, 0.52627116, 0.19547215, 0.03007264],\n",
       "       [0.78523221, 0.5769053 , 0.22435206, 0.01602515],\n",
       "       [0.80212413, 0.54690282, 0.23699122, 0.03646019],\n",
       "       [0.80779568, 0.53853046, 0.23758697, 0.03167826],\n",
       "       [0.80033301, 0.56023311, 0.20808658, 0.04801998],\n",
       "       [0.86093857, 0.44003527, 0.24871559, 0.0573959 ],\n",
       "       [0.78609038, 0.57170209, 0.23225397, 0.03573138],\n",
       "       [0.78889479, 0.55222635, 0.25244633, 0.09466737],\n",
       "       [0.76693897, 0.57144472, 0.28572236, 0.06015208],\n",
       "       [0.82210585, 0.51381615, 0.23978087, 0.05138162],\n",
       "       [0.77729093, 0.57915795, 0.24385598, 0.030482  ],\n",
       "       [0.79594782, 0.55370283, 0.24224499, 0.03460643],\n",
       "       [0.79837025, 0.55735281, 0.22595384, 0.03012718],\n",
       "       [0.81228363, 0.5361072 , 0.22743942, 0.03249135],\n",
       "       [0.76701103, 0.35063361, 0.51499312, 0.15340221],\n",
       "       [0.74549757, 0.37274878, 0.52417798, 0.17472599],\n",
       "       [0.75519285, 0.33928954, 0.53629637, 0.16417236],\n",
       "       [0.75384916, 0.31524601, 0.54825394, 0.17818253],\n",
       "       [0.7581754 , 0.32659863, 0.5365549 , 0.17496355],\n",
       "       [0.72232962, 0.35482858, 0.57026022, 0.16474184],\n",
       "       [0.72634846, 0.38046824, 0.54187901, 0.18446945],\n",
       "       [0.75916547, 0.37183615, 0.51127471, 0.15493173],\n",
       "       [0.76301853, 0.33526572, 0.53180079, 0.15029153],\n",
       "       [0.72460233, 0.37623583, 0.54345175, 0.19508524],\n",
       "       [0.76923077, 0.30769231, 0.53846154, 0.15384615],\n",
       "       [0.73923462, 0.37588201, 0.52623481, 0.187941  ],\n",
       "       [0.78892752, 0.28927343, 0.52595168, 0.13148792],\n",
       "       [0.73081412, 0.34743622, 0.56308629, 0.16772783],\n",
       "       [0.75911707, 0.3931142 , 0.48800383, 0.17622361],\n",
       "       [0.76945444, 0.35601624, 0.50531337, 0.16078153],\n",
       "       [0.70631892, 0.37838513, 0.5675777 , 0.18919257],\n",
       "       [0.75676497, 0.35228714, 0.53495455, 0.13047672],\n",
       "       [0.76444238, 0.27125375, 0.55483721, 0.18494574],\n",
       "       [0.76185188, 0.34011245, 0.53057542, 0.14964948],\n",
       "       [0.6985796 , 0.37889063, 0.56833595, 0.21312598],\n",
       "       [0.77011854, 0.35349703, 0.50499576, 0.16412362],\n",
       "       [0.74143307, 0.29421947, 0.57667016, 0.17653168],\n",
       "       [0.73659895, 0.33811099, 0.56754345, 0.14490471],\n",
       "       [0.76741698, 0.34773582, 0.51560829, 0.15588157],\n",
       "       [0.76785726, 0.34902603, 0.51190484, 0.16287881],\n",
       "       [0.76467269, 0.31486523, 0.53976896, 0.15743261],\n",
       "       [0.74088576, 0.33173989, 0.55289982, 0.18798594],\n",
       "       [0.73350949, 0.35452959, 0.55013212, 0.18337737],\n",
       "       [0.78667474, 0.35883409, 0.48304589, 0.13801311],\n",
       "       [0.76521855, 0.33391355, 0.52869645, 0.15304371],\n",
       "       [0.77242925, 0.33706004, 0.51963422, 0.14044168],\n",
       "       [0.76434981, 0.35581802, 0.51395936, 0.15814134],\n",
       "       [0.70779525, 0.31850786, 0.60162596, 0.1887454 ],\n",
       "       [0.69333409, 0.38518561, 0.57777841, 0.1925928 ],\n",
       "       [0.71524936, 0.40530797, 0.53643702, 0.19073316],\n",
       "       [0.75457341, 0.34913098, 0.52932761, 0.16893434],\n",
       "       [0.77530021, 0.28304611, 0.54147951, 0.15998258],\n",
       "       [0.72992443, 0.39103094, 0.53440896, 0.16944674],\n",
       "       [0.74714194, 0.33960997, 0.54337595, 0.17659719],\n",
       "       [0.72337118, 0.34195729, 0.57869695, 0.15782644],\n",
       "       [0.73260391, 0.36029701, 0.55245541, 0.1681386 ],\n",
       "       [0.76262994, 0.34186859, 0.52595168, 0.1577855 ],\n",
       "       [0.76986879, 0.35413965, 0.5081134 , 0.15397376],\n",
       "       [0.73544284, 0.35458851, 0.55158213, 0.1707278 ],\n",
       "       [0.73239618, 0.38547167, 0.53966034, 0.15418867],\n",
       "       [0.73446047, 0.37367287, 0.5411814 , 0.16750853],\n",
       "       [0.75728103, 0.3542121 , 0.52521104, 0.15878473],\n",
       "       [0.78258054, 0.38361791, 0.4603415 , 0.16879188],\n",
       "       [0.7431482 , 0.36505526, 0.5345452 , 0.16948994],\n",
       "       [0.65387747, 0.34250725, 0.62274045, 0.25947519],\n",
       "       [0.69052512, 0.32145135, 0.60718588, 0.22620651],\n",
       "       [0.71491405, 0.30207636, 0.59408351, 0.21145345],\n",
       "       [0.69276796, 0.31889319, 0.61579374, 0.1979337 ],\n",
       "       [0.68619022, 0.31670318, 0.61229281, 0.232249  ],\n",
       "       [0.70953708, 0.28008043, 0.61617694, 0.1960563 ],\n",
       "       [0.67054118, 0.34211284, 0.61580312, 0.23263673],\n",
       "       [0.71366557, 0.28351098, 0.61590317, 0.17597233],\n",
       "       [0.71414125, 0.26647062, 0.61821183, 0.19185884],\n",
       "       [0.69198788, 0.34599394, 0.58626751, 0.24027357],\n",
       "       [0.71562645, 0.3523084 , 0.56149152, 0.22019275],\n",
       "       [0.71576546, 0.30196356, 0.59274328, 0.21249287],\n",
       "       [0.71718148, 0.31640359, 0.58007326, 0.22148252],\n",
       "       [0.6925518 , 0.30375079, 0.60750157, 0.24300063],\n",
       "       [0.67767924, 0.32715549, 0.59589036, 0.28041899],\n",
       "       [0.69589887, 0.34794944, 0.57629125, 0.25008866],\n",
       "       [0.70610474, 0.3258945 , 0.59747324, 0.1955367 ],\n",
       "       [0.69299099, 0.34199555, 0.60299216, 0.19799743],\n",
       "       [0.70600618, 0.2383917 , 0.63265489, 0.21088496],\n",
       "       [0.72712585, 0.26661281, 0.60593821, 0.18178146],\n",
       "       [0.70558934, 0.32722984, 0.58287815, 0.23519645],\n",
       "       [0.68307923, 0.34153961, 0.59769433, 0.24395687],\n",
       "       [0.71486543, 0.25995106, 0.62202576, 0.18567933],\n",
       "       [0.73122464, 0.31338199, 0.56873028, 0.20892133],\n",
       "       [0.69595601, 0.3427843 , 0.59208198, 0.21813547],\n",
       "       [0.71529453, 0.31790868, 0.59607878, 0.17882363],\n",
       "       [0.72785195, 0.32870733, 0.56349829, 0.21131186],\n",
       "       [0.71171214, 0.35002236, 0.57170319, 0.21001342],\n",
       "       [0.69594002, 0.30447376, 0.60894751, 0.22835532],\n",
       "       [0.73089855, 0.30454106, 0.58877939, 0.1624219 ],\n",
       "       [0.72766159, 0.27533141, 0.59982915, 0.18683203],\n",
       "       [0.71578999, 0.34430405, 0.5798805 , 0.18121266],\n",
       "       [0.69417747, 0.30370264, 0.60740528, 0.2386235 ],\n",
       "       [0.72366005, 0.32162669, 0.58582004, 0.17230001],\n",
       "       [0.69385414, 0.29574111, 0.63698085, 0.15924521],\n",
       "       [0.73154399, 0.28501714, 0.57953485, 0.21851314],\n",
       "       [0.67017484, 0.36168166, 0.59571097, 0.2553047 ],\n",
       "       [0.69804799, 0.338117  , 0.59988499, 0.196326  ],\n",
       "       [0.71066905, 0.35533453, 0.56853524, 0.21320072],\n",
       "       [0.72415258, 0.32534391, 0.56672811, 0.22039426],\n",
       "       [0.69997037, 0.32386689, 0.58504986, 0.25073566],\n",
       "       [0.73337886, 0.32948905, 0.54206264, 0.24445962],\n",
       "       [0.69052512, 0.32145135, 0.60718588, 0.22620651],\n",
       "       [0.69193502, 0.32561648, 0.60035539, 0.23403685],\n",
       "       [0.68914871, 0.33943145, 0.58629069, 0.25714504],\n",
       "       [0.72155725, 0.32308533, 0.56001458, 0.24769876],\n",
       "       [0.72965359, 0.28954508, 0.57909015, 0.22005426],\n",
       "       [0.71653899, 0.3307103 , 0.57323119, 0.22047353],\n",
       "       [0.67467072, 0.36998072, 0.58761643, 0.25028107],\n",
       "       [0.69025916, 0.35097923, 0.5966647 , 0.21058754]])"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sklearn.preprocessing import Normalizer\n",
    "Normalizer().fit_transform(iris.data)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f82233a2-dd36-421c-bf2d-e4122839f760",
   "metadata": {},
   "source": [
    "### 特征二值化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "0d763141-6ea6-4f70-bdde-768498a8318a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 1., 0., 0.],\n",
       "       [1., 0., 0., 0.],\n",
       "       [1., 1., 0., 0.],\n",
       "       [1., 1., 0., 0.],\n",
       "       [1., 1., 0., 0.],\n",
       "       [1., 1., 0., 0.],\n",
       "       [1., 1., 0., 0.],\n",
       "       [1., 1., 0., 0.],\n",
       "       [1., 0., 0., 0.],\n",
       "       [1., 1., 0., 0.],\n",
       "       [1., 1., 0., 0.],\n",
       "       [1., 1., 0., 0.],\n",
       "       [1., 0., 0., 0.],\n",
       "       [1., 0., 0., 0.],\n",
       "       [1., 1., 0., 0.],\n",
       "       [1., 1., 0., 0.],\n",
       "       [1., 1., 0., 0.],\n",
       "       [1., 1., 0., 0.],\n",
       "       [1., 1., 0., 0.],\n",
       "       [1., 1., 0., 0.],\n",
       "       [1., 1., 0., 0.],\n",
       "       [1., 1., 0., 0.],\n",
       "       [1., 1., 0., 0.],\n",
       "       [1., 1., 0., 0.],\n",
       "       [1., 1., 0., 0.],\n",
       "       [1., 0., 0., 0.],\n",
       "       [1., 1., 0., 0.],\n",
       "       [1., 1., 0., 0.],\n",
       "       [1., 1., 0., 0.],\n",
       "       [1., 1., 0., 0.],\n",
       "       [1., 1., 0., 0.],\n",
       "       [1., 1., 0., 0.],\n",
       "       [1., 1., 0., 0.],\n",
       "       [1., 1., 0., 0.],\n",
       "       [1., 1., 0., 0.],\n",
       "       [1., 1., 0., 0.],\n",
       "       [1., 1., 0., 0.],\n",
       "       [1., 1., 0., 0.],\n",
       "       [1., 0., 0., 0.],\n",
       "       [1., 1., 0., 0.],\n",
       "       [1., 1., 0., 0.],\n",
       "       [1., 0., 0., 0.],\n",
       "       [1., 1., 0., 0.],\n",
       "       [1., 1., 0., 0.],\n",
       "       [1., 1., 0., 0.],\n",
       "       [1., 0., 0., 0.],\n",
       "       [1., 1., 0., 0.],\n",
       "       [1., 1., 0., 0.],\n",
       "       [1., 1., 0., 0.],\n",
       "       [1., 1., 0., 0.],\n",
       "       [1., 1., 1., 0.],\n",
       "       [1., 1., 1., 0.],\n",
       "       [1., 1., 1., 0.],\n",
       "       [1., 0., 1., 0.],\n",
       "       [1., 0., 1., 0.],\n",
       "       [1., 0., 1., 0.],\n",
       "       [1., 1., 1., 0.],\n",
       "       [1., 0., 1., 0.],\n",
       "       [1., 0., 1., 0.],\n",
       "       [1., 0., 1., 0.],\n",
       "       [1., 0., 1., 0.],\n",
       "       [1., 0., 1., 0.],\n",
       "       [1., 0., 1., 0.],\n",
       "       [1., 0., 1., 0.],\n",
       "       [1., 0., 1., 0.],\n",
       "       [1., 1., 1., 0.],\n",
       "       [1., 0., 1., 0.],\n",
       "       [1., 0., 1., 0.],\n",
       "       [1., 0., 1., 0.],\n",
       "       [1., 0., 1., 0.],\n",
       "       [1., 1., 1., 0.],\n",
       "       [1., 0., 1., 0.],\n",
       "       [1., 0., 1., 0.],\n",
       "       [1., 0., 1., 0.],\n",
       "       [1., 0., 1., 0.],\n",
       "       [1., 0., 1., 0.],\n",
       "       [1., 0., 1., 0.],\n",
       "       [1., 0., 1., 0.],\n",
       "       [1., 0., 1., 0.],\n",
       "       [1., 0., 1., 0.],\n",
       "       [1., 0., 1., 0.],\n",
       "       [1., 0., 1., 0.],\n",
       "       [1., 0., 1., 0.],\n",
       "       [1., 0., 1., 0.],\n",
       "       [1., 0., 1., 0.],\n",
       "       [1., 1., 1., 0.],\n",
       "       [1., 1., 1., 0.],\n",
       "       [1., 0., 1., 0.],\n",
       "       [1., 0., 1., 0.],\n",
       "       [1., 0., 1., 0.],\n",
       "       [1., 0., 1., 0.],\n",
       "       [1., 0., 1., 0.],\n",
       "       [1., 0., 1., 0.],\n",
       "       [1., 0., 1., 0.],\n",
       "       [1., 0., 1., 0.],\n",
       "       [1., 0., 1., 0.],\n",
       "       [1., 0., 1., 0.],\n",
       "       [1., 0., 1., 0.],\n",
       "       [1., 0., 0., 0.],\n",
       "       [1., 0., 1., 0.],\n",
       "       [1., 1., 1., 0.],\n",
       "       [1., 0., 1., 0.],\n",
       "       [1., 0., 1., 0.],\n",
       "       [1., 0., 1., 0.],\n",
       "       [1., 0., 1., 0.],\n",
       "       [1., 0., 1., 0.],\n",
       "       [1., 0., 1., 0.],\n",
       "       [1., 0., 1., 0.],\n",
       "       [1., 0., 1., 0.],\n",
       "       [1., 1., 1., 0.],\n",
       "       [1., 1., 1., 0.],\n",
       "       [1., 0., 1., 0.],\n",
       "       [1., 0., 1., 0.],\n",
       "       [1., 0., 1., 0.],\n",
       "       [1., 0., 1., 0.],\n",
       "       [1., 1., 1., 0.],\n",
       "       [1., 0., 1., 0.],\n",
       "       [1., 1., 1., 0.],\n",
       "       [1., 0., 1., 0.],\n",
       "       [1., 0., 1., 0.],\n",
       "       [1., 1., 1., 0.],\n",
       "       [1., 0., 1., 0.],\n",
       "       [1., 0., 1., 0.],\n",
       "       [1., 0., 1., 0.],\n",
       "       [1., 1., 1., 0.],\n",
       "       [1., 1., 1., 0.],\n",
       "       [1., 0., 1., 0.],\n",
       "       [1., 0., 1., 0.],\n",
       "       [1., 0., 1., 0.],\n",
       "       [1., 0., 1., 0.],\n",
       "       [1., 0., 1., 0.],\n",
       "       [1., 1., 1., 0.],\n",
       "       [1., 0., 1., 0.],\n",
       "       [1., 0., 1., 0.],\n",
       "       [1., 0., 1., 0.],\n",
       "       [1., 0., 1., 0.],\n",
       "       [1., 1., 1., 0.],\n",
       "       [1., 1., 1., 0.],\n",
       "       [1., 0., 1., 0.],\n",
       "       [1., 1., 1., 0.],\n",
       "       [1., 1., 1., 0.],\n",
       "       [1., 1., 1., 0.],\n",
       "       [1., 0., 1., 0.],\n",
       "       [1., 1., 1., 0.],\n",
       "       [1., 1., 1., 0.],\n",
       "       [1., 0., 1., 0.],\n",
       "       [1., 0., 1., 0.],\n",
       "       [1., 0., 1., 0.],\n",
       "       [1., 1., 1., 0.],\n",
       "       [1., 0., 1., 0.]])"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sklearn.preprocessing import Binarizer\n",
    "Binarizer(threshold=3).fit_transform(iris.data)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c2138876-2808-4fc8-855b-d5d19fa27ae9",
   "metadata": {},
   "source": [
    "### one-hot编码"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2241e9e8-05e3-4558-ae47-80a1974a4680",
   "metadata": {},
   "source": [
    "one-hot编码是一种对离散特征值的编码方式，在LR模型中常用到，用于给线性模型添加非线性能力。\n",
    "    对于离散特征，例如，性别：{男，女}，可以采用one-hot编码的方式将特征表示为一个m维向量，其中m为特征的取值个数\n",
    "使用preproccessing库的OneHotEncoder类对数据进行one-hot编码的代码如下："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "868cc7e3-7028-4549-86fc-3f2ee81ee53e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<150x3 sparse matrix of type '<class 'numpy.float64'>'\n",
       "\twith 150 stored elements in Compressed Sparse Row format>"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sklearn.preprocessing import OneHotEncoder\n",
    "OneHotEncoder().fit_transform(iris.target.reshape((-1,1)))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "41397c8b-88b0-4e1d-ac2e-e6677763dda5",
   "metadata": {},
   "source": [
    "### 缺失值计算"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7b6cf633-0398-449d-b2c5-fff814a4b2f0",
   "metadata": {},
   "source": [
    "在实际项目中，数据往往都是不完整的，我们可以采取以下方法进行处理：\n",
    "    删除：删除含有缺失值的数据，缺点是可能会导致信息丢失；不全：通过已有数据计算相应特征的平均值、中位数、众数来不全缺失值；建立一个模型来“预测”缺失值；\n",
    "虚拟变量：引入虚拟变量来表征是否有确实，是否有补全；\n",
    "用preproccessing库的Imputer类对数据进行缺失值的计算，代码如下："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "51b6dc9c-be76-4a40-a4b0-c11950c5ec69",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[5.84333333, 3.05733333, 3.758     , 1.19933333],\n",
       "       [5.1       , 3.5       , 1.4       , 0.2       ],\n",
       "       [4.9       , 3.        , 1.4       , 0.2       ],\n",
       "       [4.7       , 3.2       , 1.3       , 0.2       ],\n",
       "       [4.6       , 3.1       , 1.5       , 0.2       ],\n",
       "       [5.        , 3.6       , 1.4       , 0.2       ],\n",
       "       [5.4       , 3.9       , 1.7       , 0.4       ],\n",
       "       [4.6       , 3.4       , 1.4       , 0.3       ],\n",
       "       [5.        , 3.4       , 1.5       , 0.2       ],\n",
       "       [4.4       , 2.9       , 1.4       , 0.2       ],\n",
       "       [4.9       , 3.1       , 1.5       , 0.1       ],\n",
       "       [5.4       , 3.7       , 1.5       , 0.2       ],\n",
       "       [4.8       , 3.4       , 1.6       , 0.2       ],\n",
       "       [4.8       , 3.        , 1.4       , 0.1       ],\n",
       "       [4.3       , 3.        , 1.1       , 0.1       ],\n",
       "       [5.8       , 4.        , 1.2       , 0.2       ],\n",
       "       [5.7       , 4.4       , 1.5       , 0.4       ],\n",
       "       [5.4       , 3.9       , 1.3       , 0.4       ],\n",
       "       [5.1       , 3.5       , 1.4       , 0.3       ],\n",
       "       [5.7       , 3.8       , 1.7       , 0.3       ],\n",
       "       [5.1       , 3.8       , 1.5       , 0.3       ],\n",
       "       [5.4       , 3.4       , 1.7       , 0.2       ],\n",
       "       [5.1       , 3.7       , 1.5       , 0.4       ],\n",
       "       [4.6       , 3.6       , 1.        , 0.2       ],\n",
       "       [5.1       , 3.3       , 1.7       , 0.5       ],\n",
       "       [4.8       , 3.4       , 1.9       , 0.2       ],\n",
       "       [5.        , 3.        , 1.6       , 0.2       ],\n",
       "       [5.        , 3.4       , 1.6       , 0.4       ],\n",
       "       [5.2       , 3.5       , 1.5       , 0.2       ],\n",
       "       [5.2       , 3.4       , 1.4       , 0.2       ],\n",
       "       [4.7       , 3.2       , 1.6       , 0.2       ],\n",
       "       [4.8       , 3.1       , 1.6       , 0.2       ],\n",
       "       [5.4       , 3.4       , 1.5       , 0.4       ],\n",
       "       [5.2       , 4.1       , 1.5       , 0.1       ],\n",
       "       [5.5       , 4.2       , 1.4       , 0.2       ],\n",
       "       [4.9       , 3.1       , 1.5       , 0.2       ],\n",
       "       [5.        , 3.2       , 1.2       , 0.2       ],\n",
       "       [5.5       , 3.5       , 1.3       , 0.2       ],\n",
       "       [4.9       , 3.6       , 1.4       , 0.1       ],\n",
       "       [4.4       , 3.        , 1.3       , 0.2       ],\n",
       "       [5.1       , 3.4       , 1.5       , 0.2       ],\n",
       "       [5.        , 3.5       , 1.3       , 0.3       ],\n",
       "       [4.5       , 2.3       , 1.3       , 0.3       ],\n",
       "       [4.4       , 3.2       , 1.3       , 0.2       ],\n",
       "       [5.        , 3.5       , 1.6       , 0.6       ],\n",
       "       [5.1       , 3.8       , 1.9       , 0.4       ],\n",
       "       [4.8       , 3.        , 1.4       , 0.3       ],\n",
       "       [5.1       , 3.8       , 1.6       , 0.2       ],\n",
       "       [4.6       , 3.2       , 1.4       , 0.2       ],\n",
       "       [5.3       , 3.7       , 1.5       , 0.2       ],\n",
       "       [5.        , 3.3       , 1.4       , 0.2       ],\n",
       "       [7.        , 3.2       , 4.7       , 1.4       ],\n",
       "       [6.4       , 3.2       , 4.5       , 1.5       ],\n",
       "       [6.9       , 3.1       , 4.9       , 1.5       ],\n",
       "       [5.5       , 2.3       , 4.        , 1.3       ],\n",
       "       [6.5       , 2.8       , 4.6       , 1.5       ],\n",
       "       [5.7       , 2.8       , 4.5       , 1.3       ],\n",
       "       [6.3       , 3.3       , 4.7       , 1.6       ],\n",
       "       [4.9       , 2.4       , 3.3       , 1.        ],\n",
       "       [6.6       , 2.9       , 4.6       , 1.3       ],\n",
       "       [5.2       , 2.7       , 3.9       , 1.4       ],\n",
       "       [5.        , 2.        , 3.5       , 1.        ],\n",
       "       [5.9       , 3.        , 4.2       , 1.5       ],\n",
       "       [6.        , 2.2       , 4.        , 1.        ],\n",
       "       [6.1       , 2.9       , 4.7       , 1.4       ],\n",
       "       [5.6       , 2.9       , 3.6       , 1.3       ],\n",
       "       [6.7       , 3.1       , 4.4       , 1.4       ],\n",
       "       [5.6       , 3.        , 4.5       , 1.5       ],\n",
       "       [5.8       , 2.7       , 4.1       , 1.        ],\n",
       "       [6.2       , 2.2       , 4.5       , 1.5       ],\n",
       "       [5.6       , 2.5       , 3.9       , 1.1       ],\n",
       "       [5.9       , 3.2       , 4.8       , 1.8       ],\n",
       "       [6.1       , 2.8       , 4.        , 1.3       ],\n",
       "       [6.3       , 2.5       , 4.9       , 1.5       ],\n",
       "       [6.1       , 2.8       , 4.7       , 1.2       ],\n",
       "       [6.4       , 2.9       , 4.3       , 1.3       ],\n",
       "       [6.6       , 3.        , 4.4       , 1.4       ],\n",
       "       [6.8       , 2.8       , 4.8       , 1.4       ],\n",
       "       [6.7       , 3.        , 5.        , 1.7       ],\n",
       "       [6.        , 2.9       , 4.5       , 1.5       ],\n",
       "       [5.7       , 2.6       , 3.5       , 1.        ],\n",
       "       [5.5       , 2.4       , 3.8       , 1.1       ],\n",
       "       [5.5       , 2.4       , 3.7       , 1.        ],\n",
       "       [5.8       , 2.7       , 3.9       , 1.2       ],\n",
       "       [6.        , 2.7       , 5.1       , 1.6       ],\n",
       "       [5.4       , 3.        , 4.5       , 1.5       ],\n",
       "       [6.        , 3.4       , 4.5       , 1.6       ],\n",
       "       [6.7       , 3.1       , 4.7       , 1.5       ],\n",
       "       [6.3       , 2.3       , 4.4       , 1.3       ],\n",
       "       [5.6       , 3.        , 4.1       , 1.3       ],\n",
       "       [5.5       , 2.5       , 4.        , 1.3       ],\n",
       "       [5.5       , 2.6       , 4.4       , 1.2       ],\n",
       "       [6.1       , 3.        , 4.6       , 1.4       ],\n",
       "       [5.8       , 2.6       , 4.        , 1.2       ],\n",
       "       [5.        , 2.3       , 3.3       , 1.        ],\n",
       "       [5.6       , 2.7       , 4.2       , 1.3       ],\n",
       "       [5.7       , 3.        , 4.2       , 1.2       ],\n",
       "       [5.7       , 2.9       , 4.2       , 1.3       ],\n",
       "       [6.2       , 2.9       , 4.3       , 1.3       ],\n",
       "       [5.1       , 2.5       , 3.        , 1.1       ],\n",
       "       [5.7       , 2.8       , 4.1       , 1.3       ],\n",
       "       [6.3       , 3.3       , 6.        , 2.5       ],\n",
       "       [5.8       , 2.7       , 5.1       , 1.9       ],\n",
       "       [7.1       , 3.        , 5.9       , 2.1       ],\n",
       "       [6.3       , 2.9       , 5.6       , 1.8       ],\n",
       "       [6.5       , 3.        , 5.8       , 2.2       ],\n",
       "       [7.6       , 3.        , 6.6       , 2.1       ],\n",
       "       [4.9       , 2.5       , 4.5       , 1.7       ],\n",
       "       [7.3       , 2.9       , 6.3       , 1.8       ],\n",
       "       [6.7       , 2.5       , 5.8       , 1.8       ],\n",
       "       [7.2       , 3.6       , 6.1       , 2.5       ],\n",
       "       [6.5       , 3.2       , 5.1       , 2.        ],\n",
       "       [6.4       , 2.7       , 5.3       , 1.9       ],\n",
       "       [6.8       , 3.        , 5.5       , 2.1       ],\n",
       "       [5.7       , 2.5       , 5.        , 2.        ],\n",
       "       [5.8       , 2.8       , 5.1       , 2.4       ],\n",
       "       [6.4       , 3.2       , 5.3       , 2.3       ],\n",
       "       [6.5       , 3.        , 5.5       , 1.8       ],\n",
       "       [7.7       , 3.8       , 6.7       , 2.2       ],\n",
       "       [7.7       , 2.6       , 6.9       , 2.3       ],\n",
       "       [6.        , 2.2       , 5.        , 1.5       ],\n",
       "       [6.9       , 3.2       , 5.7       , 2.3       ],\n",
       "       [5.6       , 2.8       , 4.9       , 2.        ],\n",
       "       [7.7       , 2.8       , 6.7       , 2.        ],\n",
       "       [6.3       , 2.7       , 4.9       , 1.8       ],\n",
       "       [6.7       , 3.3       , 5.7       , 2.1       ],\n",
       "       [7.2       , 3.2       , 6.        , 1.8       ],\n",
       "       [6.2       , 2.8       , 4.8       , 1.8       ],\n",
       "       [6.1       , 3.        , 4.9       , 1.8       ],\n",
       "       [6.4       , 2.8       , 5.6       , 2.1       ],\n",
       "       [7.2       , 3.        , 5.8       , 1.6       ],\n",
       "       [7.4       , 2.8       , 6.1       , 1.9       ],\n",
       "       [7.9       , 3.8       , 6.4       , 2.        ],\n",
       "       [6.4       , 2.8       , 5.6       , 2.2       ],\n",
       "       [6.3       , 2.8       , 5.1       , 1.5       ],\n",
       "       [6.1       , 2.6       , 5.6       , 1.4       ],\n",
       "       [7.7       , 3.        , 6.1       , 2.3       ],\n",
       "       [6.3       , 3.4       , 5.6       , 2.4       ],\n",
       "       [6.4       , 3.1       , 5.5       , 1.8       ],\n",
       "       [6.        , 3.        , 4.8       , 1.8       ],\n",
       "       [6.9       , 3.1       , 5.4       , 2.1       ],\n",
       "       [6.7       , 3.1       , 5.6       , 2.4       ],\n",
       "       [6.9       , 3.1       , 5.1       , 2.3       ],\n",
       "       [5.8       , 2.7       , 5.1       , 1.9       ],\n",
       "       [6.8       , 3.2       , 5.9       , 2.3       ],\n",
       "       [6.7       , 3.3       , 5.7       , 2.5       ],\n",
       "       [6.7       , 3.        , 5.2       , 2.3       ],\n",
       "       [6.3       , 2.5       , 5.        , 1.9       ],\n",
       "       [6.5       , 3.        , 5.2       , 2.        ],\n",
       "       [6.2       , 3.4       , 5.4       , 2.3       ],\n",
       "       [5.9       , 3.        , 5.1       , 1.8       ]])"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from numpy import vstack, array, nan\n",
    "from sklearn import impute\n",
    "from sklearn.impute import SimpleImputer\n",
    "# 缺失值计算，返回为计算缺失值后的数据\n",
    "# 参数missing_value为缺失值的表示形式，默认为NaN\n",
    "# 对数据集新增一个样本，4个特征均赋值为NaN，表示数据缺失\n",
    "# 参数strategy为缺失填充方式，默认为mean(均值)\n",
    "SimpleImputer().fit_transform(vstack((array([nan, nan, nan, nan]), iris.data)))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3d063ada-a207-47e0-a45e-96680a32275a",
   "metadata": {},
   "source": [
    "### 数据集划分"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "713e06e4-d11f-4c53-af61-e477f8d036f5",
   "metadata": {},
   "source": [
    "在预处理阶段，还需要对数据集进行划分，包括用来训练模型的训练集、以及测试时所使用的测试集，sklearn中的model_selection为我们提供了划分数据集的方法\n",
    "鸢尾花数据集划分："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "4a11abdf-a285-4f10-bd11-e405ce1b8cbc",
   "metadata": {},
   "outputs": [],
   "source": [
    "import sklearn.model_selection as sk_model_selection\n",
    "X_train,X_test,y_train,y_test = sk_model_selection.train_test_split(iris_X,iris_y,train_size=1/3,random_state=0)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2b5998cb-dea1-402d-987e-963849bfb2ce",
   "metadata": {},
   "source": [
    "### 训练模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "849854d3-884c-4eb8-aa18-24667fd4406f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "预测的结果 [2 1 0 2 0 2 0 1 1 1 1 1 1 1 1 0 1 1 0 0 1 1 0 0 1 0 0 1 1 0 2 1 0 1 2 1 0\n",
      " 1 1 1 2 0 2 0 0 1 2 2 1 1 1 2 2 1 1 1 1 1 2 2 1 0 1 1 1 1 1 2 0 0 1 1 0 0\n",
      " 2 0 1 1 0 1 2 1 0 2 2 2 2 0 0 2 2 0 2 0 1 2 0 0 2 0]\n",
      "实际的结果 [2 1 0 2 0 2 0 1 1 1 2 1 1 1 1 0 1 1 0 0 2 1 0 0 2 0 0 1 1 0 2 1 0 2 2 1 0\n",
      " 1 1 1 2 0 2 0 0 1 2 2 2 2 1 2 1 1 2 2 2 2 1 2 1 0 2 1 1 1 1 2 0 0 2 1 0 0\n",
      " 1 0 2 1 0 1 2 1 0 2 2 2 2 0 0 2 2 0 2 0 2 2 0 0 2 0]\n"
     ]
    }
   ],
   "source": [
    "#导入模型\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "#fit（）训练\n",
    "lr = LogisticRegression()\n",
    "lr.fit(X_train,y_train)\n",
    "#predict\n",
    "result = lr.predict(X_test)\n",
    "print('预测的结果',result)\n",
    "print('实际的结果',y_test)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0112f990-816d-433c-a72a-87fce8b79200",
   "metadata": {},
   "source": [
    "### 模型评估与优化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "b6f1b2d1-0033-4412-aff0-495b37fcbbfd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'C': 1.0, 'class_weight': None, 'dual': False, 'fit_intercept': True, 'intercept_scaling': 1, 'l1_ratio': None, 'max_iter': 100, 'multi_class': 'auto', 'n_jobs': None, 'penalty': 'l2', 'random_state': None, 'solver': 'lbfgs', 'tol': 0.0001, 'verbose': 0, 'warm_start': False}\n"
     ]
    }
   ],
   "source": [
    "params = lr.get_params()\n",
    "print(params)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "707ff1e6-1ec0-4ed2-8c03-f85310f1bb3b",
   "metadata": {},
   "source": [
    "### 查看模型评分"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "9b847502-0ad0-4831-a8b5-1b644c746459",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "在训练集上的准确度评分 0.92\n",
      "在测试集上的准确度评分 0.83\n"
     ]
    }
   ],
   "source": [
    "# 模型评分 准确率\n",
    "s1 = lr.score(X_train, y_train)\n",
    "s2 = lr.score(X_test, y_test)\n",
    "print(\"在训练集上的准确度评分\", s1)\n",
    "print(\"在测试集上的准确度评分\", s2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "62a58bca-083f-469b-8c1c-c5ad8fd8941b",
   "metadata": {},
   "source": [
    "### 交叉验证"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c51454af-7969-4c37-8b19-cd0698cabfe3",
   "metadata": {},
   "source": [
    "过拟合、欠拟合的模型状态判断是模型诊断中至关重要的一步，常见的方法如：交叉验证，绘制学习曲线等\n",
    "    过拟合的调优思路是增加训练的数据量，降低模型复杂度\n",
    "    欠拟合：提高特征数量和质量，增加模型复杂度\n",
    "交叉选择验证发是从数据集中按照一定贝利，随机抽取数据用于训练模型，剩下的数据集用来做测试集，\n",
    "并将模型输出的预测值与对应数据的因变量进行比较，判断并统计准确率的方法。\n",
    "sklearn中提供了cross_val_score进行交叉验证，而ShuffleSplit则用来划分数据集的。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "19d18947-37e4-45f0-8f64-1ea13f798942",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0.96666667 0.93333333 1.         0.93333333 0.93333333]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "0.9533333333333334"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X=iris_X\n",
    "y=iris_y\n",
    "from sklearn.model_selection import cross_val_score\n",
    "from sklearn.model_selection import ShuffleSplit\n",
    "cv_split = ShuffleSplit(n_splits=5, train_size=0.7, test_size=0.2)\n",
    "score_ndarray = cross_val_score(lr, X, y, cv=cv_split)\n",
    "print(score_ndarray)\n",
    "score_ndarray.mean()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1c7458d8-5f21-4ece-8287-52b2d8abca73",
   "metadata": {},
   "source": [
    "### 模型优化"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "939cfdd0-1865-4b2f-842a-73fdf59ca202",
   "metadata": {},
   "source": [
    "真正考验水平的是根据对算法的理解调节（超）参数，使模型达到最优。\n",
    "    诊断后的模型需要进行进一步调优，调优后的新模型需要重新诊断，这是一个反复迭代不断逼近的过程，需要不断的尝试，进而达到最优的状态。\n",
    "    sklearn的子模块GridSearchCV， 是用来查找最优参数的常用方法，只需要把参数的候选集输入进去，就会自动的帮你进行排列组合，然后选\n",
    "    出得分最高的那一组参数的排列。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "60068484-7d49-4689-8a0e-3dc05e7ff13f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "最佳模型参数的评分: 0.9199999999999999\n",
      "最优参数\n",
      "{'C': 1.0, 'class_weight': None, 'dual': False, 'fit_intercept': True, 'intercept_scaling': 1, 'l1_ratio': None, 'max_iter': 100, 'multi_class': 'auto', 'n_jobs': None, 'penalty': 'l2', 'random_state': None, 'solver': 'lbfgs', 'tol': 0.0001, 'verbose': 0, 'warm_start': False}\n",
      "C : 1.0\n",
      "penalty : l2\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\YFB-SERVER\\AppData\\Local\\miniconda3\\Lib\\site-packages\\sklearn\\model_selection\\_validation.py:378: FitFailedWarning: \n",
      "30 fits failed out of a total of 60.\n",
      "The score on these train-test partitions for these parameters will be set to nan.\n",
      "If these failures are not expected, you can try to debug them by setting error_score='raise'.\n",
      "\n",
      "Below are more details about the failures:\n",
      "--------------------------------------------------------------------------------\n",
      "30 fits failed with the following error:\n",
      "Traceback (most recent call last):\n",
      "  File \"C:\\Users\\YFB-SERVER\\AppData\\Local\\miniconda3\\Lib\\site-packages\\sklearn\\model_selection\\_validation.py\", line 686, in _fit_and_score\n",
      "    estimator.fit(X_train, y_train, **fit_params)\n",
      "  File \"C:\\Users\\YFB-SERVER\\AppData\\Local\\miniconda3\\Lib\\site-packages\\sklearn\\linear_model\\_logistic.py\", line 1162, in fit\n",
      "    solver = _check_solver(self.solver, self.penalty, self.dual)\n",
      "             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
      "  File \"C:\\Users\\YFB-SERVER\\AppData\\Local\\miniconda3\\Lib\\site-packages\\sklearn\\linear_model\\_logistic.py\", line 54, in _check_solver\n",
      "    raise ValueError(\n",
      "ValueError: Solver lbfgs supports only 'l2' or 'none' penalties, got l1 penalty.\n",
      "\n",
      "  warnings.warn(some_fits_failed_message, FitFailedWarning)\n",
      "C:\\Users\\YFB-SERVER\\AppData\\Local\\miniconda3\\Lib\\site-packages\\sklearn\\model_selection\\_search.py:952: UserWarning: One or more of the test scores are non-finite: [ nan 0.92  nan 0.92  nan 0.92  nan 0.92  nan 0.92  nan 0.92]\n",
      "  warnings.warn(\n"
     ]
    }
   ],
   "source": [
    "#模型目标的参数\n",
    "from sklearn.model_selection import GridSearchCV\n",
    "penaltys=['l1','l2']#l1 或l2正则化\n",
    "cs = [1.0,1.1,1.2,1.3,1.4,1.5]\n",
    "param_grid = {'penalty':penaltys,'C':cs}\n",
    "#print(param_grid)\n",
    "gsc = GridSearchCV(LogisticRegression(),param_grid)\n",
    "#print(x_train)\n",
    "gsc.fit(X_train,y_train)\n",
    "\n",
    "print('最佳模型参数的评分:',gsc.best_score_)\n",
    "print('最优参数')\n",
    "best_params = gsc.best_estimator_.get_params()\n",
    "print(best_params)\n",
    "for param_name in sorted(param_grid.keys()):\n",
    "    print(param_name,':',best_params[param_name])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5c160463-60dc-4cea-bf0d-a58cd804b8a2",
   "metadata": {},
   "source": [
    "### 预测结果"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "469e2da5-04ee-4616-875b-7e439bdaa36f",
   "metadata": {},
   "source": [
    "经过优化后的模型已经达到最优状态，准确度也很不错，下面就可以直接进行预测："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "9444abf2-dc2a-4ae3-b95e-68dd3713af95",
   "metadata": {},
   "outputs": [],
   "source": [
    "result = lr.predict_proba(X_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "62f497da-e0ae-46f2-a54c-3deb1a51768d",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0.05070834 0.4262868  0.52300486]\n",
      " [0.17688761 0.65543327 0.16767911]\n",
      " [0.87914136 0.09381157 0.02704706]\n",
      " [0.03285144 0.36504345 0.60210511]\n",
      " [0.80728394 0.1678695  0.02484656]\n",
      " [0.03082726 0.27558454 0.6935882 ]\n",
      " [0.81970887 0.15483414 0.02545699]\n",
      " [0.12732299 0.44872083 0.42395618]\n",
      " [0.10990698 0.50721607 0.38287695]\n",
      " [0.19960899 0.53700763 0.26338338]\n",
      " [0.08880751 0.56471656 0.34647593]\n",
      " [0.16040295 0.4446039  0.39499315]\n",
      " [0.16862511 0.54865377 0.28272112]\n",
      " [0.11973681 0.51315516 0.36710803]\n",
      " [0.15121193 0.51799934 0.33078873]\n",
      " [0.85224282 0.12854153 0.01921564]\n",
      " [0.15421643 0.51455771 0.33122586]\n",
      " [0.19533202 0.59352521 0.21114277]\n",
      " [0.7505792  0.22410698 0.02531382]\n",
      " [0.83768495 0.12870717 0.03360788]\n",
      " [0.08954602 0.49811266 0.41234132]\n",
      " [0.18377642 0.50760638 0.3086172 ]\n",
      " [0.79065477 0.1821449  0.02720033]\n",
      " [0.76849326 0.21302349 0.01848326]\n",
      " [0.0920946  0.49240143 0.41550397]\n",
      " [0.87130099 0.11321548 0.01548353]\n",
      " [0.80291054 0.1579677  0.03912176]\n",
      " [0.18490535 0.51921221 0.29588244]\n",
      " [0.29349665 0.60001443 0.10648891]\n",
      " [0.77326262 0.19301436 0.03372303]\n",
      " [0.07502014 0.41314152 0.51183835]\n",
      " [0.19459727 0.51246832 0.29293441]\n",
      " [0.81167587 0.16120665 0.02711748]\n",
      " [0.10236724 0.46013599 0.43749677]\n",
      " [0.0396956  0.40175032 0.55855407]\n",
      " [0.22845702 0.56401057 0.20753241]\n",
      " [0.80117859 0.15690835 0.04191306]\n",
      " [0.09924138 0.53777129 0.36298733]\n",
      " [0.18769445 0.49757147 0.31473408]\n",
      " [0.20702614 0.58151349 0.21146037]\n",
      " [0.04382222 0.36241847 0.59375931]\n",
      " [0.80562377 0.17447201 0.01990422]\n",
      " [0.04228545 0.32451581 0.63319874]\n",
      " [0.75774529 0.19913742 0.04311729]\n",
      " [0.82779283 0.14299624 0.02921092]\n",
      " [0.22946572 0.66962042 0.10091385]\n",
      " [0.06849697 0.42422672 0.50727631]\n",
      " [0.02983661 0.26217408 0.7079893 ]\n",
      " [0.08211663 0.63648663 0.28139674]\n",
      " [0.04204288 0.48806846 0.46988865]\n",
      " [0.22710108 0.59826205 0.17463687]\n",
      " [0.01747494 0.25937005 0.72315501]\n",
      " [0.14727249 0.42070948 0.43201803]\n",
      " [0.22579867 0.61431965 0.15988168]\n",
      " [0.08052793 0.50273053 0.41674155]\n",
      " [0.10470227 0.51613066 0.37916707]\n",
      " [0.14132013 0.60988348 0.24879639]\n",
      " [0.06214016 0.52224338 0.41561646]\n",
      " [0.13328332 0.42731087 0.4394058 ]\n",
      " [0.06323595 0.40459101 0.53217303]\n",
      " [0.18463282 0.41482134 0.40054584]\n",
      " [0.76673889 0.20899716 0.02426395]\n",
      " [0.07991181 0.51326022 0.40682797]\n",
      " [0.2008547  0.56775747 0.23138782]\n",
      " [0.27360972 0.51205968 0.2143306 ]\n",
      " [0.19533749 0.60314464 0.20151787]\n",
      " [0.16614086 0.49919421 0.33466493]\n",
      " [0.04572616 0.33477353 0.61950031]\n",
      " [0.83188833 0.14031836 0.02779331]\n",
      " [0.8189749  0.16774478 0.01328032]\n",
      " [0.05923977 0.47321321 0.46754701]\n",
      " [0.24992537 0.50813437 0.24194027]\n",
      " [0.77558452 0.1912425  0.03317298]\n",
      " [0.86396612 0.09625298 0.0397809 ]\n",
      " [0.1094999  0.43630073 0.45419938]\n",
      " [0.78298713 0.19599393 0.02101895]\n",
      " [0.1010337  0.46674569 0.4322206 ]\n",
      " [0.33066945 0.55159161 0.11773894]\n",
      " [0.81774865 0.16008956 0.02216179]\n",
      " [0.0939858  0.63334138 0.27267283]\n",
      " [0.02196982 0.19083999 0.78719019]\n",
      " [0.21965012 0.52730447 0.25304541]\n",
      " [0.77917968 0.20122284 0.01959748]\n",
      " [0.03202145 0.33052863 0.63744992]\n",
      " [0.03328701 0.29137682 0.67533616]\n",
      " [0.038494   0.35665787 0.60484814]\n",
      " [0.04431876 0.41714964 0.5385316 ]\n",
      " [0.84541939 0.12854655 0.02603406]\n",
      " [0.80971821 0.16667493 0.02360686]\n",
      " [0.07325669 0.3743667  0.55237661]\n",
      " [0.04814469 0.31648284 0.63537247]\n",
      " [0.64585228 0.3329652  0.02118253]\n",
      " [0.05684809 0.31204949 0.63110242]\n",
      " [0.76037294 0.2162367  0.02339036]\n",
      " [0.06939166 0.54349613 0.38711222]\n",
      " [0.04628274 0.34382579 0.60989147]\n",
      " [0.81793687 0.1649122  0.01715093]\n",
      " [0.837755   0.13914265 0.02310235]\n",
      " [0.05668809 0.40157574 0.54173617]\n",
      " [0.80938493 0.16255997 0.0280551 ]]\n"
     ]
    }
   ],
   "source": [
    "print(result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "995e595d-9c60-462a-8e3e-5801138cebea",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
